<template>
  <!-- 登录界面窗体 -->
  <div class="login">
    <!-- 登录表单 -->
    <p
      class="loginTitle"
    >应&nbsp;&nbsp;急&nbsp;&nbsp;仿&nbsp;&nbsp;真&nbsp;&nbsp;智&nbsp;&nbsp;慧&nbsp;&nbsp;平&nbsp;&nbsp;台</p>
    <!-- 登录表单 -->
    <div class="loginForm">
      <p class="loginInfo">用&nbsp;&nbsp;户&nbsp;&nbsp;登&nbsp;&nbsp;录</p>
      <el-form
       :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="ruleForm">
        <el-form-item label prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
            prefix-icon="el-icon-user"
            placeholder="请 输 入 用 户 名"
            @keypress.native.enter="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item label prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            placeholder="请 输 入 密 码"
            @keypress.native.enter="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="ruleForm.checked" style="float:left">记&nbsp;住&nbsp;密&nbsp;码</el-checkbox>
          <el-link type="primary" style="float:right">忘&nbsp;记&nbsp;密&nbsp;码&nbsp;?</el-link>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" @keyup.enter="submitForm('ruleForm')" class="loginBtn">登&nbsp;&nbsp;录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { checkUser } from "@/api/login/login";
import { encrypt, decrypt } from "@/utils/jsencrypt"; //加密解密
import { getToken, setToken, removeToken } from "@/utils/auth";
import Cookies from "js-cookie";
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
        checked: true,
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.rules.checked) {
            Cookies.set("username", this.rules.username, { expires: 30 });
            Cookies.set("password", this.rules.password, {
              expires: 30,
            });
            Cookies.set("rememberMe", this.rules.checked, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          var data = {
            name: that.ruleForm.username,
            pass: that.$md5(that.ruleForm.password),
          };
          checkUser(data)
            .then((res) => {
              console.log(res);
              setToken(res.data.token);
              that.$router.push({ path: "/home" });
              that.$message({
                message: "登录成功",
                type: "success"
              });
            })
        } else {
          that.$message.error("用户名或密码错误");
          return false;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.ruleForm = {
        username: username === undefined ? this.ruleForm.username : username,
        password:
          password === undefined ? this.ruleForm.password : (password),
        checked: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
  },
  created() {
    this.getCookie();
  },
};
</script>
<style lang='scss'>
/* 登录界面窗体 */
.login {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  flex-direction: column;
  background: url(../assets/背景.jpg) no-repeat;
  background-size: 100% 100%;
  .loginTitle {
    width: 100%;
    margin: 0 0 1.4rem 0;
    padding: 0;
    font-size: 2.6rem;
    color: #00e1eb;
    font-weight: bold;
  }
  .loginForm {
    width: 40rem;
    height: 27rem;
    background: url(../assets/框体背景.png) no-repeat;
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    flex-direction: column;
    margin: 0 0 6.4rem 0;
    .ruleForm {
      width: 80%;
      top: 6rem;
      .el-input .el-input__inner {
        background: rgba(6, 135, 205, 0.3);
        border: 1px solid #0ba1f8;
        color: whitesmoke;
        padding-left: 3.125rem;
      }
      .el-form-item .el-checkbox__inner {
        background-color: #0687cd;
        border-color: #0ba1f8;
        width: 16px;
        height: 16px;
      }
      .el-form-item .el-checkbox__label {
        color: #68889d;
        font-size: 1rem;
      }
      .el-form-item .el-link--inner {
        color: #68889d;
        font-size: 1rem;
      }
      .el-input__icon {
        color: #00e1eb;
        font-size: 1.4rem;
      }
      .el-input__prefix {
        left: 1.125rem;
      }
    }
    .loginInfo {
      width: 100%;
      height: 2rem;
      line-height: 2rem;
      font-size: 2rem;
      text-align: center;
      color: #2a9bad;
      font-family: "Courier New", Courier, monospace;
      font-weight: bold;
    }
    .loginBtn {
      width: 100%;
      height: 2.8rem;
      background: #00e1eb;
      border: 1px solid #00bfbb;
      font-size: 1.8rem;
      font-family: "Courier New", Courier, monospace;
      font-weight: bold;
      color: #0e4c65;
    }
  }
}
</style>